#!/bin/bash

set -e

# Maintain here a list which are not to be allowed to be modified in a Jenkins workspace
# Background: We want to ensure that those files are *really* staying the same during a build/test run.
# TODO: enable this test also for package-lock.json (CMK-11132)
FILES_NOT_ALLOWED_TO_BE_MODIFIED="package.json\|Cargo.lock"

MODIFIED_FILES=$(git status -s | grep '^ M' || true)
NOT_ALLOWED_TO_BE_MODIFIED_FILES=$(echo "${MODIFIED_FILES}" | grep ${FILES_NOT_ALLOWED_TO_BE_MODIFIED} || true)
echo -e "The following files are modified: \n${MODIFIED_FILES}"

if [[ -z ${NOT_ALLOWED_TO_BE_MODIFIED_FILES} ]]; then
    echo "All modified git files are allowed to be modified."
    exit 0
else
    echo -e "ERROR: The following files are modified but are not allowed to be: \n${NOT_ALLOWED_TO_BE_MODIFIED_FILES}"
    exit 1
fi
